import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import '../../../common/constant/colors.dart';
import '../../../common/constant/styles.dart';
import 'package:app_core/app_core.dart';
import 'package:pin_code_fields/pin_code_fields.dart';

import '../../../common/intl/index.dart';

typedef CodeConfirmAction = void Function(String code);
typedef CodeResendAction = Future<bool> Function();

mixin CodeConfirmActionMixin {
  ///mixin中只用声明没有实现的，子类(使用混合的类)必须提供实现，这和继承一样
  CodeConfirmAction get confirmAction;
  CodeResendAction get resendAction;
  String get email;
  String get title;
}

class EmailCodePage extends StatelessWidget {
  EmailCodePage({super.key});

  final delegate = Get.arguments as CodeConfirmActionMixin;

  final codeController = TextEditingController();

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      // appBar: APPBar.textBar('忘记密码'),
      body: SingleChildScrollView(
        child: ConstrainedBox(
          constraints: BoxConstraints(minHeight: 1000),
          child: IntrinsicHeight(
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              mainAxisSize: MainAxisSize.min,
              children: [
                // _buildTitle(),
                SizedBox(height: 24.h),
                _buildEmailText(),
                _buildCode(context),
                SizedBox(height: 24.h),
                Expanded(child: SizedBox.expand()),
                Padding(
                  padding: const EdgeInsets.only(left: 16, right: 16),
                  child: AppButton.text(
                    style: AppStyles.s16,
                    backgroundColor: AppColor.white,
                    height: 56,
                    side: const BorderSide(color: AppColor.textSecondary, width: 1),
                    onTap: () {
                      delegate.confirmAction(codeController.text);
                    },
                    // backgroundColor: AppColor.btnDisableColor,
                    text: '确定',
                  ),
                ),
                SizedBox(height: 32.h),
                AppGesture(
                  onTap: () {
                    delegate.resendAction();
                  },
                  child: Container(
                    margin: EdgeInsets.only(left: 16, right: 16),
                    width: double.infinity,
                    alignment: Alignment.center,
                    child: Text(LoginIntl.resendCode.tr, style: AppStyles.s16),
                  ),
                ),
                SizedBox(height: 24.h),
                // SizedBox(height: AppScreenInfo.safeAreaBottom.value),
              ],
            ),
          ),
        ),
      ),
    );
  }

  // 标题
  Widget _buildTitle() {
    return Padding(padding: EdgeInsets.only(left: 16), child: Text(delegate.title, style: AppStyles.s34b));
  }

  Widget _buildEmailText() {
    return Container(
      padding: const EdgeInsets.symmetric(horizontal: 10, vertical: 5),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Text("我们已经发送了验证码到", style: TextStyle(fontSize: 18, color: AppColor.textPrimary, height: 1.5)),
          const SizedBox(height: 4), // 添加一点间距
          Text(delegate.email, style: AppStyles.s14b),
        ],
      ),
    );
  }

  // Widget _buildEmailText() {
  //   return Container(
  //     padding: EdgeInsets.only(left: 16, right: 16),
  //     child: Text.rich(
  //       TextSpan(
  //         children: [
  //           TextSpan(
  //             text: LoginIntl.verificationCodeSent.tr,
  //             style: AppStyles.s16,
  //           ),
  //           TextSpan(
  //             text: delegate.email,
  //             style: AppStyles.s16.copyWith(color: AppColor.secondary),
  //           ),
  //         ],
  //       ),
  //       style: AppStyles.s16,
  //     ),
  //   );
  // }

  Widget _buildCode(BuildContext context) {
    return Builder(
      builder: (context) {
        return Padding(
          padding: EdgeInsets.only(left: 16, right: 16, top: 24),
          child: PinCodeTextField(
            controller: codeController,
            keyboardType: TextInputType.number,
            inputFormatters: [FilteringTextInputFormatter.allow(RegExp(r'[0-9]'))],
            appContext: context,
            length: 6,
            pinTheme: PinTheme(
              shape: PinCodeFieldShape.box,
              borderRadius: BorderRadius.circular(8),
              fieldHeight: 50, // 每个格子的高度
              fieldWidth: 50, // 每个格子的宽度
              borderWidth: 1,
              // inactiveFillColor: Colors.grey[200],  // 未激活的填充色
              activeColor: AppColor.textSecondary, // 激活时的边框颜色
              inactiveColor: Colors.grey, // 未激活的边框颜色
              selectedColor: AppColor.textPrimary, // 选中时的边框颜色
            ),
          ),
        );
      },
    );
  }
}
